using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'CustomerDemographic' object
	/// </summary>
	[Serializable()]
	partial class DomainCustomerDemographic : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "CustomerDemographicCollection";
		internal DomainCustomerDemographicCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainCustomerDemographic(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainCustomerDemographicCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal CustomerDemographicPrimaryKey PrimaryKey
		{
			get { return new CustomerDemographicPrimaryKey(this.CustomerTypeId); }
		}

		internal string CustomerDesc
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CustomerDescColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CustomerDescColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CustomerDescColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CustomerDescColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerDemographicCollection list = (CustomerDemographicCollection)this.ParentCol.SubDomain[Collections.CustomerDemographicCollection];
					CustomerDemographic item = list.GetItemByPK(this.CustomerTypeId);
					if (item != null) item.CustomerDesc = value;
				}
			}
		}

		internal string CustomerTypeId
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CustomerTypeIdColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CustomerTypeIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal virtual BusinessObjectList<CustomerCustomerDemo> CustomerCustomerDemoList
		{
			get
			{
				BusinessObjectList<CustomerCustomerDemo> associatedCustomerCustomerDemoList = null;
				//if (associatedCustomerCustomerDemoList == null)
				//{
					associatedCustomerCustomerDemoList = new BusinessObjectList<CustomerCustomerDemo>();
					foreach (DomainCustomerCustomerDemo CustomerCustomerDemo in base.GetChildRows(ParentCol.CustomerDemographicCustomerCustomerDemoRelation))
					{
						if (CustomerCustomerDemo.RowState != DataRowState.Deleted)
							associatedCustomerCustomerDemoList.Add(new CustomerCustomerDemo(CustomerCustomerDemo));
					}
					associatedCustomerCustomerDemoList.ObjectAdded += new BusinessObjectEventHandler(AssociatedCustomerCustomerDemoList_ObjectAdded);
					associatedCustomerCustomerDemoList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedCustomerCustomerDemoList_ObjectRemoved);
				//}
				return associatedCustomerCustomerDemoList;
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainCustomerDemographic)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.CustomerDemographicCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveCustomerDemographic(this);
		}

		#endregion

		#region Relationship Methods
		private void AssociatedCustomerCustomerDemoList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainCustomerCustomerDemo addedItem = (DomainCustomerCustomerDemo)e.BusinessObject.WrappedClass;
			DomainCustomerCustomerDemoCollection existingCustomerCustomerDemoCollection = (DomainCustomerCustomerDemoCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.CustomerCustomerDemoCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingCustomerCustomerDemoCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingCustomerCustomerDemoCollection.GetCustomerCustomerDemo(addedItem.CustomerId, addedItem.CustomerTypeId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.CustomerDemographicCustomerCustomerDemoRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedCustomerCustomerDemoList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainCustomerCustomerDemo removedItem = (DomainCustomerCustomerDemo)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.Delete();
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
